package com.cmccgz.udacs_landlord_webapp.main.device.view;

import android.content.Intent;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.cmccgz.baselibrary.base.BaseActivity;
import com.cmccgz.baselibrary.view.MyDividerItemDecoration;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.adapter.DevicesAdapter;
import com.cmccgz.udacs_landlord_webapp.bean.DeviceInfoBean;
import com.cmccgz.udacs_landlord_webapp.interfaces.OnRvItemClickListenter;
import com.cmccgz.udacs_landlord_webapp.main.device.contract.DeviceContract;
import com.cmccgz.udacs_landlord_webapp.main.device.presenter.DevicesPresenter;
import com.cmccgz.udacs_landlord_webapp.utils.ViewHelper;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class SelectDevicesActivity extends BaseActivity implements DeviceContract.View, OnRvItemClickListenter<DeviceInfoBean>, XRecyclerView.LoadingListener ,View.OnClickListener{
    private XRecyclerView rv_devices;
    private LinearLayoutManager layoutManager;
    private List<DeviceInfoBean> datas=new ArrayList<>();
    private DevicesAdapter adapter;
    private DeviceContract.Presenter presenter;
    public static final String KEY_RESULT_DEVICE="key_result_device";
    public static final String KEY_IS_ALLSELECT="key_is_allselect";
    public static final String KEY_OPENALLSELECT="key_openallselect";
    private boolean openAllSelect=false;
    @Override
    protected int setLayoutId() {
        return R.layout.activity_devices_sel;
    }

    @Override
    protected void initView() {
        initConfigs();
        ViewHelper instance = ViewHelper.instance(this);
        instance.setTitle("设备列表");
        instance.setBackTextVisable(true);
        rv_devices = ((XRecyclerView) findViewById(R.id.rv_devices));
        adapter = new DevicesAdapter(datas,mContext,openAllSelect,this);
        layoutManager = new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL,false);
        rv_devices.setLayoutManager(layoutManager);
        rv_devices.setLoadingListener(this);
        rv_devices.setRefreshProgressStyle(ProgressStyle.SysProgress);
        rv_devices.setLoadingMoreProgressStyle(ProgressStyle.Pacman);
        rv_devices.addItemDecoration(new MyDividerItemDecoration(mContext, DividerItemDecoration.VERTICAL));
        rv_devices.setAdapter(adapter);
        rv_devices.setLoadingMoreEnabled(false);
        presenter = new DevicesPresenter(this);

        refreshDatas(true);
    }

    private void initConfigs() {
        openAllSelect=getIntent().getBooleanExtra(KEY_OPENALLSELECT,false);
    }

    private void refreshDatas(boolean isShowDialog) {
        if (isShowDialog) {
            showLoadingDialog("正在加载数据...");
        }
        presenter.getDevices();
    }

    @Override
    protected void initFragment() {

    }

    @Override
    protected void beforePermission() {

    }

    @Override
    protected int setSystemBarColorId() {
        return 0;
    }

    public void update2Adapter(List<DeviceInfoBean> devices) {
        datas.clear();
        if (devices!=null) {
            datas.addAll(devices);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void getDevicesCache(List<DeviceInfoBean> devices) {
        update2Adapter(devices);
    }

    @Override
    public void getDevicesSuccess(List<DeviceInfoBean> devices) {
        update2Adapter(devices);
        dismissLoadingDialog();
        rv_devices.refreshComplete();
    }

    @Override
    public void getDevicesError(String msg) {
        showToast(msg);
        dismissLoadingDialog();
        rv_devices.refreshComplete();
    }

    @Override
    public void onItemClick(DeviceInfoBean deviceInfoBean, int position, View itemView) {
        Intent data = new Intent();
        switch (adapter.getItemViewType(position)) {
            case 0:
                data.putExtra(KEY_IS_ALLSELECT, (Serializable) datas);
                break;
            case 1:
                data.putExtra(KEY_RESULT_DEVICE,deviceInfoBean);
                break;
        }
        setResult(RESULT_OK, data);
        onBackPressed();
    }

    @Override
    public void onRefresh() {
        refreshDatas(false);
    }

    @Override
    public void onLoadMore() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_back:
                onBackPressed();
                break;
        }
    }
}
